package com.taotao.search.controller;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.pojo.TaotaoResult;
import com.taotao.search.pojo.SearchResult;
import com.taotao.search.service.ItemService;
import com.taotao.util.ExceptionUtil;

@Controller
public class ItemController {

	@Autowired
	private ItemService itemService;

	@RequestMapping("/importall")
	@ResponseBody
	public TaotaoResult importItem() {
		try {
			TaotaoResult result = itemService.importItem();
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}

	@RequestMapping("/query")
	@ResponseBody
	public TaotaoResult search(
			@RequestParam(defaultValue = "*:*") String keyword,
			@RequestParam(defaultValue = "1") Integer currentPage,
			@RequestParam(defaultValue = "30") Integer rows) {
		
		try {
			keyword=new String(keyword.getBytes("iso8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			keyword="";
		}
			
		try {
			SearchResult searchResult = itemService.queryItem(keyword,
					currentPage, rows);
			return TaotaoResult.ok(searchResult);
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}
}
